/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.*;

import org.zzz.controller.ClassController;
import org.zzz.controller.CourseController;
import org.zzz.controller.StudentCourseController;
import org.zzz.controller.UserController;
import org.zzz.entity.Class;
import org.zzz.entity.Course;
import org.zzz.entity.StudentCourse;
import org.zzz.entity.StudentVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.util.List;
import java.util.UUID;

/**
 *
 * @author AFun
 */
public class managerStuAlter extends javax.swing.JFrame {

    /**
     * Creates new form managerStuAlter
     */
    public managerStuAlter(String m_Class,String m_Nums,
                           String m_Name,String m_Sex,String m_Course) {
        initComponents();
        init(m_Class,m_Nums,m_Name,m_Sex,m_Course);         //初始化

    }
    private void init(String m_Class,String m_Nums,String m_Name,String m_Sex,String m_Course){
        this.setTitle("修改学生信息");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);

        //获取到现存的所有课程名，填入到“课程”下拉框内
        CourseController courseController = new CourseController();
        List<Course> data = courseController.listCourse().getData();
        int index=0;
        for (Course datum : data) {
            this.jComboBox1.insertItemAt(datum, index++);
        }



        //将上一个界面的信息传入
        this.m_Class=m_Class;
        this.m_Nums=m_Nums;
        this.temp_Nums = m_Nums;
        this.m_Name=m_Name;
        this.m_Sex=m_Sex;
        this.m_Course=m_Course;
        //获取全部班级
        ClassController classController = new ClassController();
        Result<List<Class>> listResult = classController.listClass();
        List<Class> classObject = listResult.getData();

        index=0;
        for (Class aClass : classObject) {
            this.jComboBox3.insertItemAt(aClass,index++);
        }

        //设置班级
        for(int i=0;i<this.jComboBox3.getItemCount();i+=1){
            if(this.jComboBox3.getItemAt(i).getName().equals(this.m_Class)){
                this.jComboBox3.setSelectedIndex(i);
                break;
            }
        }

        //设置学号
        this.jTextField1.setText(this.m_Nums);
        this.jTextField1.setEditable(false);

        this.xingming.setText(this.m_Name);
        if(this.m_Sex.equals("男")){
            this.jRadioButton1.setSelected(true);
        }else{
            this.jRadioButton2.setSelected(true);
        }

        //设置课程
        for(int i=0;i<this.jComboBox1.getItemCount();i+=1){
            if(this.jComboBox1.getItemAt(i).getName().equals(this.m_Course)){
                this.jComboBox1.setSelectedIndex(i);
                break;
            }
        }



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        xingming = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox3 = new javax.swing.JComboBox<>();
        ButtonGroup bg=new ButtonGroup();
        bg.add(jRadioButton1);
        bg.add(jRadioButton2);


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jRadioButton1.setText("男");

        jRadioButton2.setText("女");

        jLabel4.setText("班级");

        jButton1.setText("确定");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("学号");

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("姓名");

        jLabel3.setText("性别");

        jLabel5.setText("课程");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(44, 44, 44)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(jButton2))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addGap(47, 47, 47)
                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, Short.MAX_VALUE)
                                                .addComponent(jButton1))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jLabel3)
                                                                .addGap(47, 47, 47)
                                                                .addComponent(jRadioButton1)
                                                                .addGap(30, 30, 30)
                                                                .addComponent(jRadioButton2))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(jLabel2)
                                                                        .addComponent(jLabel1)
                                                                        .addComponent(jLabel4))
                                                                .addGap(47, 47, 47)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(xingming, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                                                        .addComponent(jTextField1)
                                                                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                                .addGap(0, 0, Short.MAX_VALUE)))
                                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4)
                                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(xingming, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(14, 14, 14)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(jRadioButton1)
                                        .addComponent(jRadioButton2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(90, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)
                                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        managerStuSelect mss=new managerStuSelect();
        this.dispose();
        mss.setVisible(true);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //将信息写入数据库
        if(this.jRadioButton1.isSelected()){
            this.m_Sex="男";
        }else{
            this.m_Sex="女";
        }
        UserController userController = new UserController();
        StudentCourseController studentCourseController = new StudentCourseController();
        userController.updateStudent(new StudentVo(this.m_Nums,this.xingming.getText(),this.m_Sex,
                ((Class)this.jComboBox3.getSelectedItem()).getId()));
        userController.updateUserById(this.temp_Nums,this.m_Nums);
        //        String courseName = studentCourseController.getCourseNamesBySid(this.m_Nums).getData();
        Result<StudentCourse> studentCourseResult = studentCourseController.getStudentCourseBySid(this.m_Nums);
        if(studentCourseResult.getStatus()==ResultEnum.SUCCESS.getStatus()){
            //存在学号和课程关系,获取id，修改
            StudentCourse studentCourse = studentCourseResult.getData();
            studentCourseController.updateSCById(new StudentCourse(studentCourse.getId(),
                    this.m_Nums,((Course)this.jComboBox1.getSelectedItem()).getId()));
        }else{
            //存在学号和课程关系，添加
            studentCourseController.addSC(new StudentCourse(UUID.randomUUID().toString(),
                    this.m_Nums,((Course)this.jComboBox1.getSelectedItem()).getId()));
        }
        JOptionPane.showMessageDialog(null, "修改成功！");
        //修改成功后回到上一个查询页面
        managerStuSelect mss=new managerStuSelect();
        this.dispose();
        mss.setVisible(true);


//        //是否继续修改
//        if (JOptionPane.showConfirmDialog(null, "是否修改？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
//            //是：将文本框清空
//            this.banji.setText("");
//            this.xuehao.setText("");
//            this.xingming.setText("");
//        }else{
//            //回到上一个界面
//            managerStuSelect mss = new managerStuSelect();
//            this.dispose();
//            mss.setVisible(true);
//        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(managerStuAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(managerStuAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(managerStuAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(managerStuAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new managerStuAlter("","","","","").setVisible(true);
            }
        });
    }
    private String m_Class;
    private String m_Nums;
    private String m_Name;
    private String m_Sex;
    private String m_Course;
    private String temp_Nums;



    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox<Course> jComboBox1;
    private javax.swing.JComboBox<Class> jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField xingming;
    // End of variables declaration
}
